package rad.framework.jsf;

import java.io.IOException;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.TagHandler;

/**
 * This handler is similar to c:set but it stores the actual value instead of
 * the value expression.
 * 
 * @author boudyacho
 * 
 */
public class SetHandler extends TagHandler {

	private final TagAttribute var;

	private final TagAttribute value;

	public SetHandler(TagConfig config) {
		super(config);
		this.value = this.getRequiredAttribute("value");
		this.var = this.getRequiredAttribute("var");
	}

	public void apply(FaceletContext ctx, UIComponent parent)
			throws IOException, FacesException, FaceletException, ELException {
		String varStr = this.var.getValue(ctx);
		ValueExpression veObj = this.value
				.getValueExpression(ctx, Object.class);
		ctx.setAttribute(varStr, veObj.getValue(ctx));
	}
}